/* 
* @Author: sublime text
* @Date:   2015-09-30 13:10:12
* @Last Modified by:   sublime text
* @Last Modified time: 2015-10-02 09:11:29
*/

$(document).ready(function(){

	
	var input = document.querySelector(".text input");
    var send = document.querySelector("#subxx");
    var msg_robot = document.querySelector(".outer .chatContent .msg_robot");
    var msg_mine = document.querySelector(".outer .chatContent .msg_mine");
    var chatContent = document.querySelector(".chatContent");
 
    /*机器人聊天函数
    * */
    function chat() {
        var sayContent = input.value;
        var paras = "key=347b39ee228b4b109dae7270cc08d3c8&userid=Fengmaybe&info="+sayContent;
        ajax.post("http://www.tuling123.com/openapi/api",paras,function (content,xhr) {
            //克隆mine的节点，深度克隆
            sends_message('绿巨人', 1, sayContent); // sends_message(昵称,头像id,聊天内容);
            //设置自动滚动文本聊天信息
            $('.scrollbar-macosx.scroll-content.scroll-scrolly_visible').animate({
				scrollTop: $('.scrollbar-macosx.scroll-content.scroll-scrolly_visible').prop('scrollHeight')
			}, 500);
            //只要提交了，清空input栏的值
            input.value="";
            console.log(content);
            //post响应的数据是一个json对象，所以要转成js对象
            var obj = JSON.parse(content);
            //判断响应回复的信息是哪种类型
            msgRobot = obj.text;
            
            //克隆新的节点robot的
            sends_messageAI('美国队长', 12, msgRobot);
            //设置自动滚动文本聊天信息
            $('.scrollbar-macosx.scroll-content.scroll-scrolly_visible').animate({
				scrollTop: $('.scrollbar-macosx.scroll-content.scroll-scrolly_visible').prop('scrollHeight')
			}, 500);
        });
    }
 
 
    /*让指定的元素自动滚动
    * */
    function autoScroll(parentEle) {
        setTimeout(function step() {
            //注意scrollTop不是CSS的属性是js的属性,是没有单位的
            var top = parentEle.scrollTop;
            parentEle.scrollTop += top +4;
            //设置定时器关闭的条件
            if(top == parentEle.scrollTop) return;
            setTimeout(step,20);
        },0)
    }
 
    /*为send按钮设置单机响应函数,注意这里是一个函数，只需要把函数名给他就行
    * */
    send.onclick = chat;
 
    /*设置键盘响应函数，点击enter就相当于button的单机
    *这里是要执行的，所以要chat();
    * */
    document.οnkeydοwn=function (ev) {
        if(ev.keyCode == 13){
            chat();
        }
    }





// -----下边的代码不用管---------------------------------------



	jQuery('.scrollbar-macosx').scrollbar();
	$('.topnavlist li a').click(function(event) {
		$('.topnavlist .popover').not($(this).next('.popover')).removeClass('show');
		$(this).next('.popover').toggleClass('show');
		if($(this).next('.popover').attr('class')!='popover fade bottom in') {
			$('.clapboard').removeClass('hidden');
		}else{
			$('.clapboard').click();
		}
	});
	$('.clapboard').click(function(event) {
		$('.topnavlist .popover').removeClass('show');
		$(this).addClass('hidden');
		$('.user_portrait img').attr('portrait_id', $('.user_portrait img').attr('ptimg'));
		$('.user_portrait img').attr('src', 'images/user/' + $('.user_portrait img').attr('ptimg') + '.png');
		$('.select_portrait img').removeClass('t');
		$('.select_portrait img').eq($('.user_portrait img').attr('ptimg')-1).addClass('t');
		$('.rooms .user_name input').val('');
	});
	$('.select_portrait img').hover(function() {
		var portrait_id = $(this).attr('portrait_id');
		$('.user_portrait img').attr('src', 'images/user/' + portrait_id + '.png');
	}, function() {
		var t_id = $('.user_portrait img').attr('portrait_id');
		$('.user_portrait img').attr('src', 'images/user/' + t_id + '.png');
	});
	$('.select_portrait img').click(function(event) {
		var portrait_id = $(this).attr('portrait_id');
		$('.user_portrait img').attr('portrait_id', portrait_id);
		$('.select_portrait img').removeClass('t');
		$(this).addClass('t');
	});
	$('.face_btn,.faces').hover(function() {
		$('.faces').addClass('show');
	}, function() {
		$('.faces').removeClass('show');
	});
	$('.faces img').click(function(event) {
		if($(this).attr('alt')!='') {
			$('.text input').val($('.text input').val() + '[em_' + $(this).attr('alt') + ']');
		}
		$('.faces').removeClass('show');
		$('.text input').focus();
	});
	$('.imgFileico').click(function(event) {
		$('.imgFileBtn').click();
	});
	function sends_message (userName, userPortrait, message) {
		if(message!='') {
			$('.main .chat_info').html($('.main .chat_info').html() + '<li class="right"><img src="images/user/' + userPortrait + '.png" alt=""><b>' + userName + '</b><i>09:15</i><div class="aaa">' + message  +'</div></li>');
		}
	}
	function sends_messageAI (userName, userPortrait, message) {
		if(message!='') {
			$('.main .chat_info').html($('.main .chat_info').html() + '<li class="left"><img src="images/user/' + userPortrait + '.png" alt=""><b>' + userName + '</b><i>09:15</i><div>' + message  +'</div></li>');
		}
	}
	$('.text input').keypress(function(e) { 
		if (e.which == 13){
			$('#subxx').click();
		}
	});
});
